package com.sort.insert;

public class InsertSort {
    private int[] array;

    public InsertSort(int[] array) {
        this.array = array;
    }

    public int[] sort() {

        for (int i = 1; i < array.length; i++) {
            // 欲插入的数值
            int value = array[i];
            // 欲插入的索引
            int index = i - 1;

            while (index >= 0 && value < array[index]) {
                array[index + 1] = array[index];
                index--;
            }

            if (index + 1 != i) {
                array[index + 1] = value;
            }
        }

        return this.array;
    }
}
